package com.dolphin.servicemap.remot;

import com.dolphin.common.constant.AmapConfigConstants;
import com.dolphin.common.response.DirectionResponse;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
@Slf4j
public class MapDirectionClient {

    @Value("${amap.key}")
    private String amapKey;

    @Autowired
    private RestTemplate restTemplate;

    public DirectionResponse direction(String depLongitude, String depLatitude, String destLongitude, String destLatitude){
        //组装请求调用url
        /**
         * https://restapi.amap.com/v3/direction/driving?
         * origin=116.481028,39.989643&
         * destination=116.465302,40.004717&
         * extensions=all&output=json&key=2eaf6eb9ba18dce6f8076479f9a39f4c
         */
        StringBuilder urlBuilder = new StringBuilder();
        urlBuilder.append(AmapConfigConstants.DIRECTION_URL)
                .append("?")
                .append("origin="+depLongitude+","+depLatitude)
                .append("&")
                .append("destination="+destLongitude+","+destLatitude)
                .append("&")
                .append("extensions=base")
                .append("&")
                .append("output=json")
                .append("&")
                .append("key="+amapKey);

        log.info("高德地图路径规划接口，url："+urlBuilder.toString());

        //调用高德接口
        ResponseEntity<String> directionEntity = restTemplate.getForEntity(urlBuilder.toString(), String.class);
        log.info("接口返回："+directionEntity.getBody());

        //解析接口
        DirectionResponse directionResponse = parseDirectionEntity(directionEntity.getBody());

        return directionResponse;


    }

    private DirectionResponse parseDirectionEntity(String directionString) {
        DirectionResponse directionResponse = null;
        try {

            JSONObject result = JSONObject.fromObject(directionString);
            if (result.has(AmapConfigConstants.STATUS)) {
                int status = result.getInt(AmapConfigConstants.STATUS);
                if (status == 1) {
                    if (result.has(AmapConfigConstants.ROUTE)) {
                        JSONObject route = result.getJSONObject(AmapConfigConstants.ROUTE);
                        JSONArray pathsArray = route.getJSONArray(AmapConfigConstants.PATHS);
                        JSONObject pathObject = pathsArray.getJSONObject(0);

                        directionResponse = new DirectionResponse();
                        if (pathObject.has(AmapConfigConstants.DISTANCE)) {
                            int distance = pathObject.getInt(AmapConfigConstants.DISTANCE);
                            directionResponse.setDistance(distance);
                        }
                        if (pathObject.has(AmapConfigConstants.DURATION)) {
                            int duration = pathObject.getInt(AmapConfigConstants.DURATION);
                            directionResponse.setDuration(duration);
                        }

                    }
                }

            }

        } catch (Exception e) {

        }


        return directionResponse;
    }
}
